<?php
namespace Home\Controller;

class ProductController extends HomeController {
    public function index(){

        $cateID = I('get.cateID');
        $path = I('get.path');
        $keywords = I('keywords');

        if ($cateID!='' && is_numeric($cateID)) {
            $cateName = M('Category')->where(array('model'=>2,'id'=>$cateID))->getField('name');
            if (!$cateName) {
                $this->error('分类不存在');
            }
            $this->assign('cateName',$cateName);

            $map['cid'] = $cateID;
        }

        if ($keywords!='') {
            $map['name'] = array('like','%'.$keywords.'%');
        }

        if ($path!='') {
            $cateName = M('Category')->where(array('model'=>2,'path'=>$path))->getField('name');
            if (!$cateName) {
                $this->error('分类不存在');
            }
            $this->assign('cateName',$cateName);
            $map['path'] = array('like', $path.'%');
        }

        $map['show']=1;
        $obj = M('Goods');
        $count = $obj->where($map)->count();
        import("Common.ORG.BootstrapPage");
        $page = new \Page($count, 9);
        $show = $page->show();
        $list = $obj->where($map)->order('sort asc,id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('cateID',$cateID);

        $this->display();
    }

    
    public function detail(){

        unset($map);
        $id = I('get.id');
        if ($id=='' || !is_numeric($id)) {
            $this->error('参数错误');
        }

        $map['id'] = $id;
        $map['show'] = 1;  
        $list = M('Goods')->where($map)->find();
        if (!$list) {
            $this->error('产品不存在');
        }else{
            if ($list['image']) {
                $image = array();
                $list['image'] = explode(",", $list['image']);
                foreach ($list['image'] as $key => $value) {
                    $img_info = getimagesize('./'.$value);
                    $data = array(
                        'url'=>$value,
                        'width'=>$img_info[0],
                        'height'=>$img_info[1],
                        );
                    array_push($image, $data);
                }
            }

            $tmpArr = explode("\n", $list['intr']);
            $intr = array();
            for ($i=0; $i < count($tmpArr); $i++) { 
                if (trim($tmpArr[$i])!='') {
                    array_push($intr , $tmpArr[$i]);
                }
            }

            $this->assign('image', $image);
            $this->assign('intr', $intr);
            $this->assign('list', $list);

            $category = M("Category")->where(array('model'=>2,'fid'=>0))->order('sort asc,id asc')->select();
            foreach ($category as $key => $value) {
                $category[$key]['child'] = M("Category")->where(array('fid'=>$value['id']))->order('sort asc,id asc')->select();
            }
            $this->assign('category',$category);

            $banner = M("Ad")->where(array('id'=>4))->find();
            $this->assign('banner',$banner);

            if ($list['carID']) {
                $carID = explode(",", $list['carID']);
                unset($map);
                $map['id'] = array('in',$carID);
                $car = M('Chexing')->where($map)->order('brandID asc')->select();
                foreach ($car as $key => $value) {
                    $car[$key]['info'] = M('Car')->where(array('chexingID'=>$value['id']))->select();
                }
                $this->assign("car",$car);
            }
            $this->display();
        }
    }
}